package com.sky.aspect;

import com.sky.annotation.AutoFill;
import com.sky.constant.AutoFillConstant;
import com.sky.context.BaseContext;
import com.sky.enumeration.OperationType;
import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.aspectj.lang.reflect.MethodSignature;
import org.springframework.stereotype.Component;

import java.lang.reflect.Method;
import java.time.LocalDateTime;

/**
 *自动填充的切面类
 *
 * @Author itcast
 * @Create 2023/4/25
 */
@Aspect
@Component
@Slf4j
public class AutoFillAspect {
    /**
     * 切入点
     */
    @Pointcut("execution(* com.sky.mapper.*.*(..)) && @annotation(com.sky.annotation.AutoFill)")
    public void autoFillPointCut(){}

    /**
     *通知 为公共属性赋值
     * @param joinPoint
     */
    @Before("autoFillPointCut()")
    public void autoFillAdvice(JoinPoint joinPoint) {
        log.info("公共字段自动填充...");

        //方法签名,描述的是当前被拦截的方法信息
        MethodSignature signature = (MethodSignature) joinPoint.getSignature();

        //当前方法让AutoFill注解
        AutoFill autoFill = signature.getMethod().getAnnotation(AutoFill.class);

        //获得操作类型
        OperationType operationType = autoFill.value();

        //当前方法的参数实体类型
        Object[] args = joinPoint.getArgs();
        if (args == null || args.length == 0) {
            return;
        }

        //获取实体对象
        Object entity = args[0];

        //准备赋值数据
        LocalDateTime time = LocalDateTime.now();
        Long currentId = BaseContext.getCurrentId();

        if (operationType == OperationType.INSERT) {
            //处理4个公共字段
            try {
                //获得实体中的setter方法对象
                Method setCreateTime = entity.getClass().getDeclaredMethod(AutoFillConstant.SET_CREATE_TIME, LocalDateTime.class);
                Method setUpdateTime = entity.getClass().getDeclaredMethod(AutoFillConstant.SET_UPDATE_TIME, LocalDateTime.class);
                Method setCreateUser = entity.getClass().getDeclaredMethod(AutoFillConstant.SET_CREATE_USER, Long.class);
                Method setUpdateUser = entity.getClass().getDeclaredMethod(AutoFillConstant.SET_UPDATE_USER, Long.class);
                //通过反射调用这个setter方法
                setCreateTime.invoke(entity, time);
               // entity.setCReateTime(time);
                setUpdateTime.invoke(entity, time);
                setCreateUser.invoke(entity, currentId);
                setUpdateUser.invoke(entity, currentId);
                //new Category().setCreateTime(time);
            } catch (Exception e) {
                e.printStackTrace();
            }

        } else {
            try {
                //获得实体中的setter方法对象
                Method setUpdateTime = entity.getClass().getDeclaredMethod(AutoFillConstant.SET_UPDATE_TIME, LocalDateTime.class);
                Method setUpdateUser = entity.getClass().getDeclaredMethod(AutoFillConstant.SET_UPDATE_USER, Long.class);
                //通过反射调用这个setter方法
                setUpdateTime.invoke(entity, time);
                setUpdateUser.invoke(entity, currentId);
                //new Category().setCreateTime(time);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }
}
